package soderqvist.paulsson.logmaster;

import helpers.DateConverter;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class NoteList extends ListActivity {
	private String[] erikNotes;
	private String[] johanNotes;
	private String[] timeAndPerson;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_list);
		Log.i("NOTELIST", "Notelist started");
		String notes = getIntent().getStringExtra("notes");
		if (notes.contains("<--->")) {
			notes = notes.substring(0, notes.lastIndexOf("<--->"));
		}
		String[] temp = notes.split(">---<");

		if (temp[0].length() > 10) {
			erikNotes = temp[0].split("<--->");
		} else {
			erikNotes = new String[0];
		}
		if (temp[1].length() > 10) {
			johanNotes = temp[1].split("<--->");
		} else {
			johanNotes = new String[0];
		}
		timeAndPerson = DateConverter.getTimeAndPerson(erikNotes, johanNotes);
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, timeAndPerson));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_note_list, menu);
		return true;
	}

	public void onListItemClick(ListView parent, View v, int position, long id) {
		String name = "";
		String time = "";
		String note = "";

		if (timeAndPerson[position].charAt(0) == 'E') {
			for (int i = 0; i < erikNotes.length; i++) {
				if (erikNotes[i].contains(timeAndPerson[position].substring(7))) {
					note = erikNotes[i]
							.substring(erikNotes[i].indexOf(':') + 3);
					name = timeAndPerson[position].split("-")[0].trim();
					time = timeAndPerson[position].split("-")[1].trim();
				}
			}
		} else {
			for (int i = 0; i < johanNotes.length; i++) {
				if (johanNotes[i]
						.contains(timeAndPerson[position].substring(8))) {
					note = johanNotes[i]
							.substring(johanNotes[i].indexOf(':') + 3);
					name = timeAndPerson[position].split("-")[0].trim();
					time = timeAndPerson[position].split("-")[1].trim();
				}
			}
		}
		Intent i = new Intent(this, ShowNote.class);
		i.putExtra("name", name);
		i.putExtra("time", time);
		i.putExtra("note", note);
		startActivity(i);
	}

}
